using System.Web;
using System.Xml;
using System;

using umlungu.engineX.emailX;
/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2007-11-23	
	Status:		release	
	Version:	2.0.0
	Build:		20071123
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20071123:	Moved class from x_result file
	20071123:	Added constructors for logging and email alerts.
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX {
	/// <summary>
	/// Specialised exception class.
	/// <para>This class is used to raise exceptions that have a code and message</para>
	/// </summary>
	public class x_exception : System.Exception {
		#region Invisible properties
		#endregion

		#region Constants
		private const string LOG_INFO = "code:,{0},message:,'{1}'";
		private const string LOG_DEBUG = "code:,{0},message:,'{1}',trace:,{2}";
		private const string ALERT_INFO = "x_exception code:{0}\nx_exception message:'{1}'";
		#endregion

		#region Visible properties
		private string code;
		/// <summary>The error code</summary>
		/// <value>Value of the error code allocated in construction of the exception</value>
		public string Code {
			get { return code; }
			set { code = value; }
		}
		private x_logger logger;
		/// <summary>The logger object</summary>
		/// <value>A log4net object</value>
		public x_logger Logger {
			get { return logger; }
		}
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default Constructor</summary>
		/// <param name="cde">Error code</param>
		/// <param name="message">Error message</param>
		public x_exception(string cde, string message) : base(message) {
			code = cde;
		}
		/// <summary>Constructor with type (implies logging)</summary>
		/// <param name="cde">Error code</param>
		/// <param name="message">Error message</param>
		/// <param name="type">Type to be logged</param>
		public x_exception(string cde, string message, Type type) : base(message) {
			code = cde;
			if (type != null)
				logger = new x_logger(type);
			logger.Info(String.Format(LOG_INFO, Code, Message));
			logger.Debug(String.Format(LOG_DEBUG, Code, Message, StackTrace));
		}
		/// <summary>Constructor with to,from,subject (implies alert email)</summary>
		/// <param name="cde">Error code</param>
		/// <param name="message">Error message</param>
		/// <param name="to">The destination alert email address</param>
		/// <param name="from">The originating alert email address</param>
		/// <param name="subject">The alert subject</param>
		public x_exception(string cde, string message, string to, string from, string subject) : base(message) {
			code = cde;
			sendAlert(to, from, subject);
		}
		#endregion

		#region Public methods
		/// <overloads>Sends an alert</overloads>
		/// <summary>
		/// Sends an email alert
		/// </summary>
		/// <param name="to">The destination email address</param>
		/// <param name="from">The originating email address</param>
		/// <param name="subject">The alert subject</param>
		public void Alert(string to, string from, string subject) {
			sendAlert(to, from, subject);
		}
		/// <summary>
		/// Sends a custom email alert
		/// </summary>
		/// <param name="to">The destination email address</param>
		/// <param name="from">The originating email address</param>
		/// <param name="subject">The alert subject</param>
		/// <param name="message">The alert message</param>
		public void Alert(string to, string from, string subject, string message) {
			sendAlert(to, from, subject, message);
		}
		/// <summary>
		/// Sends a SMS alert
		/// </summary>
		/// <param name="to">The destination mobile number</param>
		/// <param name="subject">The alert subject</param>
		public void Alert(string to, string subject) {
		}
		#endregion

		#region Protected methods
		#endregion

		#region Private methods
		/// <summary>
		/// Send email alert
		/// </summary>
		private void sendAlert(string to, string from, string subject) {
			x_email alert = new x_email(to, from, subject);
			alert.Send(String.Format(ALERT_INFO, Code, Message));
		}
		/// <summary>
		/// Send custom email alert
		/// </summary>
		private void sendAlert(string to, string from, string subject, string message) {
			x_email alert = new x_email(to, from, subject);
			alert.Send(message);
		}
		/// <summary>
		/// Send sms alert
		/// </summary>
		private void sendAlert(string to, string subject) {
		}
		#endregion
	}
}
